package com.clock.shop.service.impl;

import java.util.List;

import com.clock.common.enums.shop.AppointedStatus;
import com.clock.common.utils.DateUtils;
import com.clock.shop.domain.ShopAppointed;
import com.clock.shop.domain.ShopShop;
import com.clock.shop.domain.vo.AppointedVo;
import com.clock.shop.mapper.ShopAppointedMapper;
import com.clock.shop.service.IShopAppointedService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
@RequiredArgsConstructor
public class ShopAppointedServiceImpl implements IShopAppointedService
{
    @Resource
    private ShopAppointedMapper shopAppointedMapper;

    @Autowired
    private ShopShopServiceImpl shopShopService;

    //查询预约单总数
    public Long selectAppointedNum(Long shopId) {
        return shopAppointedMapper.selectAppointedNum(shopId);
    }
    /**
     * 查询预约
     * 
     * @param appointedId 预约主键
     * @return 预约
     */
    @Override
    public AppointedVo selectShopAppointedByAppointedId(Long appointedId)
    {
        // 查询出预约单信息并转化为AppointedVo对象
        AppointedVo appointedVo = shopAppointedMapper.selectShopAppointedByAppointedId(appointedId);
        // AppointedVo appointedVo = new AppointedVo();
        // BeanUtils.copyProperties(shopAppointed,appointedVo);
        // 根据预约单的用户id查询出用户信息，并将用户名封装进Appointed对象
        // SysUser sysUser = sysUserService.selectUserById(shopAppointed.getUserId());
        // appointedVo.setUserName(sysUser.getUserName());

        // 根据预约单的店铺id查询出店铺信息，并将店铺名封装进Appointed对象
        // ShopShop shopShop = shopShopService.selectShopShopByShopId(shopAppointed.getShopId());
        // appointedVo.setShopName(shopShop.getShopName());
        if(appointedVo.getAppointedStatus().equals( AppointedStatus.UNTRANSFORM.getCode())){
            appointedVo.setAppointedStatus(AppointedStatus.UNTRANSFORM.getInfo());
        }else {
            appointedVo.setAppointedStatus(AppointedStatus.TRANSFORMED.getInfo());
        }
        return appointedVo;
    }

    /**
     * 查询预约列表
     * 
     * @param appointedVo 预约
     * @return 预约
     */
    @Override
    public List<AppointedVo> selectShopAppointedList(AppointedVo appointedVo)
    {

        ShopAppointed shopAppointed = new ShopAppointed();
        BeanUtils.copyProperties(appointedVo,shopAppointed);
        // 如果查询条件中的用户名不为空，则根据用户名查询出用户id，并将用户id封装进ShopAppointed对象
        // if(appointedVo.getUserName()!=null){
        //     SysUser sysUser = sysUserService.selectUserById(shopAppointed.getUserId());
        //     shopAppointed.setUserId(sysUser.getUserId());
        // }
        // 如果查询条件中的店铺名不为空，则根据店铺名查询出店铺id，并将店铺id封装进ShopAppointed对象
      if(appointedVo.getShopName()!=null){
          List<ShopShop> shopShops = shopShopService.selectShopShopByShopName(appointedVo.getShopName());
          shopAppointed.setShopId(shopShops.get(0).getShopId());
      }
      //   List<ShopAppointed> shopAppointeds = shopAppointedMapper.selectShopAppointedList(shopAppointed);
      // List<AppointedVo> appointedVos = new ArrayList<>();
        List<AppointedVo> appointedVos = shopAppointedMapper.selectShopAppointedList(appointedVo);
      // 将ShopAppointed对象转换为AppointedVo对象，并将转换后的AppointedVo对象添加到appointedVos列表中
        for (AppointedVo appointedVo1 : appointedVos) {
            // AppointedVo appointedVo1 = new AppointedVo();
            // SysUser sysUser = sysUserService.selectUserById(shopAppointed1.getUserId());
            // appointedVo1.setUserName(sysUser.getUserName());
            // 设置预约单状态
            if(appointedVo1.getAppointedStatus()== AppointedStatus.UNTRANSFORM.getCode()){
                appointedVo1.setAppointedStatus(AppointedStatus.UNTRANSFORM.getInfo());
            }else {
                appointedVo1.setAppointedStatus(AppointedStatus.TRANSFORMED.getInfo());
            }
            // appointedVos.add(appointedVo1);
        }
        return appointedVos;
    }

    /**
     * 新增预约
     *
     * @param appointedVo 预约
     * @return 结果
     */
    @Override
    public int insertShopAppointed(AppointedVo appointedVo)
    {
        appointedVo.setCreateTime(DateUtils.getNowDate());
        return shopAppointedMapper.insertShopAppointed(appointedVo);
    }

    /**
     * 修改预约
     *
     * @param appointedVo 预约
     * @return 结果
     */
    @Override
    public int updateShopAppointed(AppointedVo appointedVo)
    {
        return shopAppointedMapper.updateShopAppointed(appointedVo);
    }

    /**
     * 批量删除预约
     * 
     * @param appointedIds 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteShopAppointedByAppointedIds(Long[] appointedIds)
    {
        return shopAppointedMapper.deleteShopAppointedByAppointedIds(appointedIds);
    }

    /**
     * 删除预约信息
     * 
     * @param appointedId 预约主键
     * @return 结果
     */
    @Override
    public int deleteShopAppointedByAppointedId(Long appointedId)
    {
        return shopAppointedMapper.deleteShopAppointedByAppointedId(appointedId);
    }
}
